package com.yj.cmp.sdk.fusioncompute.compute.host;

import com.yj.cmp.sdk.fusioncompute.common.utils.RequestUtils;
import com.yj.cmp.sdk.fusioncompute.compute.host.vo.HostDetailVo;
import com.yj.cmp.sdk.fusioncompute.compute.host.vo.HostListVo;
import com.yj.cmp.sdk.fusioncompute.compute.host.vo.HostNetworkVo;
import com.yj.cmp.sdk.fusioncompute.compute.host.vo.hostStorageVo;
import org.springframework.stereotype.Component;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Harvey_Yang
 * @date 2021/8/28 11:13
 */
@Component
public class FcHostApi {


    FcHostService fcHostService;

    public FcHostApi(FcHostService fcHostService) {
        this.fcHostService = fcHostService;
    }

    /**
     * 查询主机列表
     */
    public List<HostListVo.HostsDTO> hostList(Map<String, Object> headers, Map<String, Object> queryParam) throws Exception {

        Response<HostListVo> response = fcHostService.hostList(headers, queryParam).execute();
        RequestUtils.isSuccessful(response);
        List<HostListVo.HostsDTO> list = new ArrayList<>(response.body().getHosts());
        if (response.body().getTotal() <= 100) {
            if (null != response.body()) {
                return response.body().getHosts();
            }
            return new ArrayList<>(0);
        }

        int page = (int) Math.ceil(new Double(response.body().getTotal()) / 100);
        for (int i = 2; i <= page; i++) {
            queryParam.put("offset", i);
            queryParam.put("limit", 100);
            list.addAll(fcHostService.hostList(headers, queryParam).execute().body().getHosts());
        }
        return list;
    }

    /**
     * 查询主机详细
     */
    public HostDetailVo hostDetail(Map<String, Object> headerMap, String hostId) throws Exception {
        Response<HostDetailVo> response = fcHostService.hostDetail(headerMap, hostId).execute();
        RequestUtils.isSuccessful(response);
        if (null != response.body()) {
            return response.body();
        }
        return new HostDetailVo();
    }


    /**
     * 查询主机关联的存储资源
     */
    public List<hostStorageVo.DatastoresDTO> hostStorage(Map<String, Object> headers, Map<String, Object> queryParam) throws Exception {

        Response<hostStorageVo> response = fcHostService.hostStorage(headers, queryParam).execute();
        RequestUtils.isSuccessful(response);
        List<hostStorageVo.DatastoresDTO> list = new ArrayList<>(response.body().getDatastores());
        if (response.body().getTotal() <= 100) {
            if (null != response.body()) {
                return response.body().getDatastores();
            }
            return new ArrayList<>(0);
        }

        int page = (int) Math.ceil(new Double(response.body().getTotal()) / 100);
        for (int i = 2; i <= page; i++) {
            queryParam.put("offset", i);
            queryParam.put("limit", 100);
            list.addAll(fcHostService.hostStorage(headers, queryParam).execute().body().getDatastores());
        }
        return list;
    }

    /**
     * 查询主机关联的网络资源
     */
    public List<HostNetworkVo.DvSwitchsDTO> hostNetworkResource(Map<String, Object> headerMap, Map<String, Object> queryParam) throws Exception {
        Response<HostNetworkVo> response = fcHostService.hostNetworkResource(headerMap, queryParam).execute();
        RequestUtils.isSuccessful(response);
        if (null != response.body()) {
            return response.body().getDvSwitchs();
        }
        return new ArrayList<>(0);
    }

}
